package com.kevin.ymqquan.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.kevin.ymqquan.R;
import com.kevin.ymqquan.application.App;
import com.kevin.ymqquan.constants.Constants;
import com.kevin.ymqquan.custom.CustomToast;
import com.kevin.ymqquan.httppost.Helper;
import com.kevin.ymqquan.httppost.HttpPostService;

public class CourseApplyReasonActivity extends Activity {

	private App application;
	private String courseId;
	private String isJoin = "0";
	private ImageView iv_clubapplyreason_back;
	private EditText et_clubapplyreason_text;

	private Dialog dialog;
	private Button btn_clubapplyreason_send;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_club_apply_reason);

		Intent i = getIntent();
		courseId = i.getStringExtra("courseId");
		init();
	}

	private void init() {
		// TODO Auto-generated method stub
		application = (App) getApplication();
		iv_clubapplyreason_back = (ImageView) findViewById(R.id.iv_clubapplyreason_back);
		et_clubapplyreason_text = (EditText) findViewById(R.id.et_clubapplyreason_text);

		btn_clubapplyreason_send = (Button) findViewById(R.id.btn_clubapplyreason_send);

		btn_clubapplyreason_send.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub

				if ("".equals(et_clubapplyreason_text.getText().toString())) {
					CustomToast.CustomActivityToast(
							CourseApplyReasonActivity.this, "请输入您的申请理由", 0);
				} else {
					if (Helper.checkConnection(CourseApplyReasonActivity.this)) {
						dialog = CustomToast.createLoadingDialog(
								CourseApplyReasonActivity.this, "提交中...");
						dialog.setCancelable(true);
						dialog.show();
						ClubApplyReasonAsyTask suggestionTask = new ClubApplyReasonAsyTask();
						suggestionTask.execute();

					} else {
						CustomToast.CustomActivityToast(
								CourseApplyReasonActivity.this, "网络连接异常，请检查您的网络",
								-80);
					}
				}

			}
		});

		iv_clubapplyreason_back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				 CourseApplyReasonActivity.this.finish();
			}
		});
	}
	
	@Override  
    public boolean onKeyDown(int keyCode, KeyEvent event) {  
        if(keyCode == KeyEvent.KEYCODE_BACK){  
        	backIntent();
        }  
        return false;  
    }  
	
	private void backIntent(){
		Intent intent = new Intent(CourseApplyReasonActivity.this,CourseDetailActivity.class);
		intent.putExtra("isJoin", isJoin+"");
		setResult(RESULT_OK,intent);
		CourseApplyReasonActivity.this.finish();
	}

	class ClubApplyReasonAsyTask extends AsyncTask<String, Object, String> {

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub

			// sendReplyText = Base64.encodeToString(sendReplyText.getBytes(),
			// Base64.NO_WRAP);

			// et_suggestion_text.setFilters(new InputFilter[]{new
			// InputFilter(){
			// Pattern emoji =
			// Pattern.compile("[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]"
			// ,Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);
			// @Override
			// public CharSequence filter(CharSequence source, int start,
			// int end, Spanned dest, int dstart, int dend) {
			// // TODO Auto-generated method stub
			// Matcher emojiMatcher = emoji.matcher(source) ;
			// if ( emojiMatcher.find()) {
			// return "";
			// }
			// return null;
			// }
			// }
			// });
			String applyReason = et_clubapplyreason_text.getText().toString();
			// sendReplyText = Base64.encodeToString(sendReplyText.getBytes(),
			// Base64.NO_WRAP);

			String param = "{\"method\":\"courseSignUp\",\"userId\":\""+application.userId+"\",\"courseId\":\""+courseId+"\",\"applyReason\":\""+applyReason+"\"}";
			String replyList = "";
			try {
				replyList = HttpPostService.PostJsonString(
						Constants.ServerAddr, param);
			} catch (Exception e) {
				e.printStackTrace();
			}
//			Log.i("kevinlog", param);
//			Log.i("kevinlog", replyList);
			return replyList;
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			Log.i("kevinlog", result);
			JSONObject jsonObject;
			try {
				jsonObject = new JSONObject(result);
				String resultType = jsonObject.getString("status");
				if ("0".equals(resultType)) {
					CustomToast.CustomActivityToast(
							CourseApplyReasonActivity.this, "申请成功，等待教练审核", 0);
					isJoin = "1";
					backIntent();
					CourseApplyReasonActivity.this.finish();
				} else {
					CustomToast.CustomActivityToast(
							CourseApplyReasonActivity.this, "发送失败，请检查您的网络", 0);
				}

			} catch (JSONException e) {
				// TODO Auto-generated catch block
				Log.i("kevinlog", "error");
				e.printStackTrace();
			}

			dialog.dismiss();
		}

	}
}
